# INSTRUCTIONS -----------------------------------------------------------------
# Ensure you have R version 4.2.0 or later installed. The script uses:
#   - The native pipe operator (`|>`) and placeholder (`_`)
#   - The new anonymous function shorthand (`\(x)`)
# 
# To run the analysis, simply execute this script. It will automatically call 
# the necessary functions and run the code for you.
#
# If you prefer to run each script manually, use `main_alt.R` instead.

# Clear the workspace by removing all existing objects
rm(list = ls())

# Set your working directory to the project folder.
setwd(getwd())

# Create the directories to export the plots and tables
dir.create(file.path(getwd(), "plots"), showWarnings = FALSE)
dir.create(file.path(getwd(), "tables"), showWarnings = FALSE)

# Load required libraries from the 'code/libraries.R' script
source('code/libraries.R')

# Import custom functions from the 'code/functions.R' script
source('code/functions.R')

# Load datasets as data frames and store them in a list
datasets <- list(
  "1" = read.csv("data/clean_data_1.csv", stringsAsFactors = TRUE),
  "2" = read.csv("data/clean_data_2.csv", stringsAsFactors = TRUE),
  "3" = read.csv("data/clean_data_3.csv", stringsAsFactors = TRUE)
)

# Loop through each dataset and run the data analysis
for (name in names(datasets)) {
  analyze_data(datasets[[name]], name)
}

# Clear the workspace by removing all existing objects
rm(list = ls())
